$(document).ready(
	function ()
	{
//		do_focus();

		$("#tfsbox_input_dialog").draggable();
		$("#tfsbox_msg_dialog").draggable();

		$("input[title]").tooltip(
		{
			events: {input: "focus, blur"},

 			// place tooltip on the right edge
			position: "center right",

			// a little tweaking of the position
			offset: [10, 7],

			// use the built-in fadeIn/fadeOut effect
			effect: "slide",

			// custom opacity setting
			opacity: 0.9,

			// use this single tooltip element
			tip: '#tfsbox_tooltip'
		});

//		do_focus(); // called twice to work around the tooltip's bug.

		tfsbox_apply_toggle();

		$(".tfsbox_menu").mousedown(
			function (e)
			{
				$(".tfsbox_menu").css("z-index", "3");
				$(e.currentTarget).css("z-index", "5");
			}
		);

		$(".tfsbox_subscriptions > ul li div").mousedown(
			function (e)
			{
				e.stopPropagation();
			}
		);

		$(".tfsbox_subscriptions > div").height($(".tfsbox_subscriptions ul").first().height());
	}
);

function change_settings(form_name)
	{
		var form = $("form[name=" + form_name + "]");
		form.children("input[type=text]").hide();
		var select_field = form.children("select").first();
		form.children("input[name=" + select_field.val() + "]").show();
		form.children("input[name=" + select_field.val() + "]").focus();
	}

function detach(e, pid)
	{
		var left = $(e).offset().left;
		var top = $("#"+pid).offset().top;
		$(e).hide();
		$("#"+pid).appendTo("body");
		$("#"+pid).draggable();
		$("#"+pid).css("cursor", "move");
		$("#"+pid).show();
		$("#"+pid).css("left", left + "px");
		$("#"+pid).css("top", top + "px");
	}

function do_focus()
	{
		if ($("input[name=tfsbox_name]").val() == '')
			$("input[name=tfsbox_name]").focus();
		else
			$("input[name=tfsbox_shout]").focus();

		if ($("input[name=tfsbox_username]").val() == '')
			$("input[name=tfsbox_username]").focus();
		else
			$("input[name=tfsbox_password]").focus();
	}

function edit_shout(shout_id, action)
	{
		$.ajax(
		{
			type: "POST",
			url: action,
			data: "tfsbox_shout_id=" + shout_id,
			success: function(shout)
				{
					if (shout.length)
						show_input_dialog(action, shout_id, "post", shout, "TFsBOX: Edit Shout");
					else
						show_error_dialog("No shout to edit.");
				},
			error: function(XMLHttpRequest, textStatus, errorThrown)
				{
					show_error_dialog("There was an error retreiving the shout: " + textStatus);
				}
		});
	}

function get_subscription(e, user, action)
	{
		$(".tfsbox_subscriptions > div").hide();
		$(".tfsbox_subscriptions > ul li.active").removeClass("active");
		$(e).addClass("active");
		if ($(".tfsbox_subscriptions > ul li.active div.shouts").text().length == 1)
		{
			$(".tfsbox_subscriptions > ul li.active div.shouts").html("<span style=\"text-decoration: blink;\">Loading...</span>");
			$.ajax(
			{
				type: "GET",
				url: action,
				data: user,
				success: function(shouts)
					{
						if (shouts.length)
							$(".tfsbox_subscriptions > ul li.active div.shouts").html(shouts);
						else
							show_error_dialog("No shouts to retrieve.");
					},
				error: function(XMLHttpRequest, textStatus, errorThrown)
					{
						show_error_dialog("There was an error retreiving the subscription: " + textStatus);
					}
			});
		}
	}

function hide_dialog(id)
	{
		$("#"+id).hide("blind", {direction: "vertical"});
	}

function hide_input_dialog()
	{
		$("#screen").fadeOut();
		$("#tfsbox_input_dialog").fadeOut();
	}

function hide_msg_dialog()
	{
		$("#screen").fadeOut();
		$("#tfsbox_msg_dialog").fadeOut();
	}

function refresh_shouts(action, force)
	{
		if (!$("#tfsbox_refresh_shouts_cb").is(":checked") && !force)
		{
			return false;
		}

		$.ajax(
		{
			url: action,
			success: function(data)
				{
					var text = $(data);
					$("#tfsbox_content").html(text.html());
					tfsbox_apply_toggle();
				},
			error: function(XMLHttpRequest, textStatus, errorThrown)
				{
					show_error_dialog("There was an error retreiving the shouts: " + textStatus);
				}
		});
	}

function show_error_dialog(content)
	{
		$("#tfsbox_error_dialog_content").html(content);
		$("#tfsbox_error_dialog").show("slide", {direction: "up"});
	}

function show_input_dialog(action, shout_id, method, message, title)
	{
		$("form#tfsbox_input_dialog").attr("action", action);
		$("form#tfsbox_input_dialog").attr("method", method);
		var content = "<input type=\"hidden\" name=\"tfsbox_shout_id\" value=\"" + shout_id + "\">\n";
		content += "Shout: <input class=\"tfsbox_textfield\" type=\"text\" name=\"tfsbox_shout\" value=\"" + message + "\">\n";
		$("#tfsbox_input_dialog_header").text(title);
		$("#tfsbox_input_dialog_content").html(content);
		var bw = $(window).width();
		var bh = $(window).height();
		var dw = $("#tfsbox_input_dialog").outerWidth();
		var dh = $("#tfsbox_input_dialog").outerHeight();
		$("#tfsbox_input_dialog").css("left", (bw/2 - dw/2) + "px");
		$("#tfsbox_input_dialog").css("top", (bh/2 - dh/2) + "px");
		$("#screen").fadeIn();
		$("#screen").css("width", bw);
		$("#screen").css("height", $("html").outerHeight());
		$("#tfsbox_input_dialog").fadeIn();
		$("form#tfsbox_input_dialog input[name=tfsbox_shout]").focus();
	}

function show_msg_dialog(message, title)
	{
		title = title || $("#tfsbox_msg_dialog_header").text();
		$("#tfsbox_msg_dialog_header").text(title);
		$("#tfsbox_msg_dialog_text").html(message);
		var bw = $(window).width();
		var bh = $(window).height();
		var dw = $("#tfsbox_msg_dialog").outerWidth();
		var dh = $("#tfsbox_msg_dialog").outerHeight();
		$("#tfsbox_msg_dialog").css("margin-left", 0);
		$("#tfsbox_msg_dialog").css("left", (bw/2 - dw/2) + "px");
		$("#tfsbox_msg_dialog").css("top", (bh/2 - dh/2) + "px");
		$("#screen").fadeIn();
		$("#screen").css("width", bw);
		$("#screen").css("height", $("html").outerHeight());
		$("#tfsbox_msg_dialog").fadeIn();
	}

function show_notice_dialog(content)
	{
		$("#tfsbox_notice_dialog_content").html(content);
		$("#tfsbox_notice_dialog").show("slide", {direction: "up"});
	}

function tfsbox_add_shout(action)
	{
		$.ajax(
		{
			type: "POST",
			url: action,
			data: $("#tfsbox_shoutform").serialize(),
			success: function (data)
				{
					var text = $(data);

					if (data.indexOf('<errors>') > -1)
					{
						var error_msg = '<ul>\n';
						text.find("error").each(
							function ()
							{
								error_msg += '<li>' + $(this).html() + '</li>\n';
							}
						);
						error_msg += '</ul>\n';

						show_error_dialog(error_msg);
					}

					else
					{
						if (data.indexOf('<notices>') > -1)
						{
							var notices = '<ul>\n';
							text.find("notice").each(
								function ()
								{
									notices += '<li>' + $(this).html() + '</li>\n';
								}
							);
							notices += '</ul>\n';

							show_notice_dialog(notices);
						}

						$("#tfsbox_content").html(text.find("#tfsbox_shouts").parent().html());
						tfsbox_apply_toggle();
						$("input[name=tfsbox_name]").attr("readonly", true);
						$("input[name=tfsbox_name]")[0].defaultValue = $("input[name=tfsbox_name]").val();
						$("input[name=tfsbox_shout]").val("");
						do_focus();
					}
				},
			error: function (XMLHttpRequest, textStatus, errorThrown)
				{
					show_error_dialog("There was an error retreiving the shouts: " + textStatus);
				}
		});
	}

function tfsbox_apply_toggle()
	{
		$("*[id^=tfsbox_shouts] > div").toggle(
			function ()
			{
				$(this).addClass("active").children("div.tfsbox_shout_options").show("blind", {}, 100);
			},

			function ()
			{
				$(this).removeClass("active").children("div.tfsbox_shout_options").hide("blind", {}, 100);
			}
		);

		$("*[id^=tfsbox_shouts] > div a").click(
			function (e)
			{
				e.stopPropagation();
			}
		);
	}

function tfsbox_insert_smiley(smiley)
	{
		$("input[name=tfsbox_shout]").val($("input[name=tfsbox_shout]").val() + smiley);
		do_focus();
	}
